package com.my.mybatis.plugins;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Properties;

/**
 * @Author:wangbin.
 * @Description.
 * @Date:Created in 14:57 2017/9/13.
 * @Modified By:
 */
@Intercepts({@Signature(
        type= Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MyPlugins implements Interceptor {

    public static Logger log = Logger.getLogger(MyPlugins.class);

    private MyPlugins myPlugins;

    public MyPlugins() {
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Object object = invocation.getArgs()[1];
        BoundSql boundSql = mappedStatement.getBoundSql(object);
        String sql = boundSql.getSql();
        Object obj = boundSql.getParameterObject();
        List<ParameterMapping> list = boundSql.getParameterMappings();
        System.out.println("my plugins   ===================================   sql = " + sql);
        log.debug("sql = " + sql);
        System.out.println("my plugins   ===================================   obj = " + obj);
        log.debug("parameter = " + obj);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        this.myPlugins = new MyPlugins();

    }
}
